package com.tour.controller.portal;

import com.tour.common.Const;
import com.tour.common.ResponseCode;
import com.tour.common.ServerResponse;
import com.tour.pojo.User;
import com.tour.service.IUserService;
import com.sun.corba.se.spi.activation.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by tang
 */
@Controller
@RequestMapping("/user/")
public class UserController {


    @Resource
    private IUserService iUserService;
    
    //临时验证码
    private String tempCode = null;


    /**
     * 用户登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String username, String password, HttpSession session){
        ServerResponse<User> response = iUserService.login(username,password);
        System.out.println(username+"  "+password);
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
            session.setAttribute("param", "pafarefsdfsdzf");
        }
        return response;
    }

    /**
     * 退出登录，并跳转到登录页面
     * @param session
     * @return
     */
    @RequestMapping(value = "logout.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess();
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping(value = "register.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> register(User user,@RequestParam String validCode){
        return iUserService.register(user,validCode);
    }

    /**
     * 邮箱发送验证码
     * @param validCode
     * @return
     */
    @RequestMapping(value = "getCheckEmail.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> getCheckEmail(@RequestParam String email){
    	//临时作品
    	if((tempCode=iUserService.sendCheckMes(email))!=null){
    		return ServerResponse.createBySuccessMessage("验证码发送成功");
    	}
		return ServerResponse.createByErrorMessage("验证码发送失败");
    	
    }
    
    /**
     * 发送短信
     * @param validCode
     * @return
     */
    @RequestMapping(value = "getCheckCode.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> getcheckCode(@RequestParam String phone){
    	//临时作品
    	if((tempCode=iUserService.sendCheckMes(phone))!=null){
    		return ServerResponse.createBySuccessMessage("验证码发送成功");
    	}
		return ServerResponse.createByErrorMessage("验证码发送失败");
    	
    }
    
    /**
     * 判断验证码
     * @param validCode
     * @return
     */
    @RequestMapping(value = "doCheckCode.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> doCheckCode(@RequestParam String validCode){
    	System.out.println("================validCode="+validCode);
    	if(tempCode.equals(validCode)){
    		return ServerResponse.createBySuccessMessage("验证码匹配成功");
    	}
    	return ServerResponse.createByErrorMessage("验证码匹配失败");
    	
    }

    @RequestMapping(value = "check_valid.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkValid(String str,String type){
        return iUserService.checkValid(str,type);
    }


    @RequestMapping(value = "get_user_info.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> getUserInfo(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user != null){
            return ServerResponse.createBySuccess(user);
        }
        return ServerResponse.createByErrorMessage("用户未登录,无法获取当前用户的信息");
    }


    @RequestMapping(value = "forget_get_question.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetGetQuestion(String username){
        return iUserService.selectQuestion(username);
    }


    @RequestMapping(value = "forget_check_answer.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetCheckAnswer(String username,String question,String answer){
        return iUserService.checkAnswer(username,question,answer);
    }

    /**
     * 验证用户昵称是否存在
     * @param phone
     * @return
     */
    @RequestMapping(value = "check_username.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkUsername(@RequestParam String username){
  
		return iUserService.checkUsername(username);
    	
    }
    
   
    /**
     * 验证手机号是否存在
     * @param phone
     * @return
     */
    @RequestMapping(value = "check_phone.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkPhone(@RequestParam String phone){
  
		return iUserService.checkPhone(phone);
    	
    }
    /**
     * 忘记密码
     * @param username
     * @param passwordNew
     * @param forgetToken
     * @return
     */
/*    @RequestMapping(value = "forget_reset_password.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetRestPassword(String phone,String newpassword,String forgetToken){
        return iUserService.forgetResetPassword(phone,newpassword,forgetToken);
    }*/
        @RequestMapping(value = "forget_reset_password.do",method = RequestMethod.POST)
        @ResponseBody
    public ServerResponse<String> forgetRestPassword(String phone,String newpassword,String validcode){
        return iUserService.forgetResetPassword(phone,newpassword,validcode);
    }
    

   /**验证旧密码
    * 
    * @param session
    * @param oldpassword
    * @return
    */
    @RequestMapping(value = "check_password.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkPassword(HttpSession session,String oldpassword){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        
        return iUserService.checkPassword(oldpassword, user);
    }
    
   /**
    * 修改密码
    * @param session
    * @param oldpassword
    * @param newpassword
    * @return
    */
    @RequestMapping(value = "reset_password.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> resetPassword(HttpSession session,String newpassword){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        
        return iUserService.resetPassword(newpassword,user);
    }


    @RequestMapping(value = "update_information.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> update_information(HttpSession session,User user){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return ServerResponse.createByErrorMessage("用户未登录");
        }
        user.setUserId(currentUser.getUserId());
        user.setUsername(currentUser.getUsername());
        ServerResponse<User> response = iUserService.updateInformation(user);
        if(response.isSuccess()){
            response.getData().setUsername(currentUser.getUsername());
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }

    @RequestMapping(value = "get_information.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> get_information(HttpSession session){
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"未登录,需要强制登录status=10");
        }
        return iUserService.getInformation(currentUser.getUserId());
    }
}
